////////////////////////////////////////////////////////////////////////////////
//
//  MICROSOFT CORPORATION
//  Copyright (c) Microsoft Corporation.
//  All Rights Reserved.
//
//  NOTICE: Microsoft Confidential. Intended for Internal Use Only.
//
////////////////////////////////////////////////////////////////////////////////

package com.msn.beet.tracking
{

import com.adobe.errors.IllegalStateError;
import com.msn.beet.core.StaticClass;

import flash.utils.Dictionary;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

/**
 * Static handler for routing ITrackActions to correct ISender
 * implementations.
 */
public class Track extends StaticClass
{
    include "../core/Version.as";

    protected static var _senders:Dictionary = new Dictionary();

    /**
     * Adds an ISender implementation to Track's collection of ISenders.
     * @param sender:ITrackSender
     */
    public static function addSender(sender:ISender):void
    {
        var className:String = getQualifiedClassName(sender);
        var classDef:Class = getDefinitionByName(className) as Class;

        if (_senders[classDef])
        {
            throw new ArgumentError("ISender instance already added.");
        }
        else
        {
            _senders[classDef] = sender;
        }
    }

    /**
     * Returns the instance of the senderType from Track's collection,
     * returns null if the instance is not found.
     * @param senderType:Class the sender class type
     * @return * the requested tracker, or null if not found
     */
    public static function getSender(senderType:Class):ISender
    {
        if (_senders[senderType])
        {
            return _senders[senderType];
        }
        else
        {
            return null;
        }
    }


    /**
     * Removes and returns the instance of the senderType from Track's
     * collection,
     * returns null if the instance is not found.
     * @param sender:ITrackSender
     */
    public static function removeSender(senderType:Class):ISender
    {

        if (_senders[senderType])
        {
            var sender:ISender = _senders[senderType];
            delete _senders[senderType];
            return sender;
        }
        else
        {
            return null;
        }
    }

    /**
     * Sends a tracking request through all available tracking senders,
     * throws error if there are no senders.
     * @param trackAction
     */
    public static function send(trackAction:ITrackAction):void
    {
        for each (var sender:ISender in _senders)
        {
            sender.send(trackAction);
        }
    }

}

}
